﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;
using ESCAPI_WRAPPER;


namespace escapi_winforms
{
    //  C# code by Thorbear. Original ESCAPI library by Sol of sol.gfxile.net/   
    
    public partial class Form1 : Form
    {
        private Bitmap bm;

        private SimpleCapParams capture;

        private uint active_device = 0;

        public Form1()
        {
            InitializeComponent();
            
            EasyFunctions.initWebcam(640, 480, ref capture, ref pictureBox1, ref bm);
            
            // Make our webcam frame fit within the form
            this.Size = new System.Drawing.Size(capture.mWidth + 100, capture.mHeight + 20);
            
            // start grabbing frames on each timer tick
            captureTimer.Interval = 10;
            captureTimer.Start();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            // First, capture the image given the capture params and a bitmap to write to
            EasyFunctions.captureWebcamFrame(active_device, ref capture, ref bm);

            // Lastly, display out bitmap
            pictureBox1.Image = bm;

        }


        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            captureTimer.Stop();

            Escapi.deinitCapture(active_device);

            capture.freeMem();
        }

    }
}
